package com.dilidili.service.impl;

import com.dilidili.mapper.UserMapper;
import com.dilidili.service.IUserService;
import com.util.AesUtil;
import com.domain.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @PackgeName: com.dilidili.service.impl
 * @ClassName: UserServiceImpl
 * @Author: 零
 * Date: 2022/4/3 20:42
 * project name: spring-cloud-mybilibli
 * @Version:
 * @Description:
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * @param userTel 手机号
     * @return
     */
    @Override
    public List<Map<String, Object>> query(String userTel) {
        return userMapper.query(userTel);
    }

    /**
     * @param userTel 手机号
     * @return
     */
    @Override
    public User userPhone(String userTel) {
        User user = userMapper.userPhone(userTel);
        String secretKey="1111111111111111";
        String password = AesUtil.decrypt(user.getUserPassword(), secretKey);
        user.setUserPassword(password);
        return user;
    }

    /**
     * 将修改过的密码加密
     *
     * @param userTel      手机号
     * @param userPassword 密码
     * @return
     */
    @Override
    public Integer amendPswd(String userTel, String userPassword) {
        //解密查出来的密码
        String secretKey="1111111111111111";
        String password = AesUtil.encrypt(userPassword, secretKey);
        return userMapper.amendPswd(userTel, password);
    }

    /**
     * @param userId 用户id
     * @return
     */
    @Override
    public Integer fansByUserId(Integer userId) {
        return userMapper.fansByUserId(userId);
    }

    /**
     * 用户粉丝数据
     *
     * @param userId 用户对象
     * @return
     */
    @Override
    public Integer beanByUserId(Integer userId) {
        return userMapper.beanByUserId(userId);
    }

    /**
     * 用户数据处理
     *
     * @param user 数据对象
     * @return
     */
    @Override
    public Integer saveUser(User user){
        //用户密码加密使用SHA加密
        String secretKey="1111111111111111";
        String password = AesUtil.encrypt(user.getUserPassword(), secretKey);
        user.setUserCreatetime(new Date());
        String s = UUID.randomUUID().toString().substring(0, 9);
        s.substring(1, 8);
        user.setUserNickname(s);
        user.setUserImgsrc("https://jiangaibo.oss-cn-beijing.aliyuncs.com/bbb/255191664/cover.jpg");
        user.setUserSuggest("这家伙很懒！什么也没留下");
        user.setUserPassword(password);
        return userMapper.saveUser(user);
    }

    /**
     * 修改用户数据处理
     *
     * @param user 用户对象
     * @return 响应结果
     */
    @Override
    public Integer modification(User user) {
        String secretKey="1111111111111111";
        String password = AesUtil.encrypt(user.getUserPassword(), secretKey);
        user.setUserPassword(password);
        return userMapper.modification(user);
    }

    /**
     * 查询用户昵称
     *
     * @param userNickname 用户昵称
     * @return 用户昵称数据
     */
    @Override
    public List<Map<String, Object>> userGetUserNickname(Integer userId, String userNickname) {
        return userMapper.userGetUserNickname(userId, userNickname);
    }

    /**
     * 用户数据
     *
     * @param userId   用户参数
     * @param pageNum  分页查询
     * @param pageSize
     * @return 返回数据
     */
    @Override
    public PageInfo<Map> queryFansParticulars(Integer userId, String userNickname, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map> list = userMapper.queryFansParticulars(userId, userNickname);
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * @param userId
     * @param userNickname
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<User> queryFansBeanParticulars(Integer userId, String userNickname, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.queryFansBeanParticulars(userId, userNickname);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 用户数据
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public User findAll(Integer userId) {
        return userMapper.findAll(userId);
    }
}
